package com.jh.paymentchannel.service;

import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jh.paymentchannel.pojo.SDJBindCard;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.jh.paymentchannel.business.TopupPayChannelBusiness;
import com.jh.paymentchannel.util.Util;
import com.jnewsdk.util.StringUtils;

import net.sf.json.JSONObject;
@Controller
@EnableAutoConfiguration
public class SDJInfoPage {
	 
	private static final Logger LOG = LoggerFactory.getLogger(SDJInfoPage.class);
	
	@Autowired
	private TopupPayChannelBusiness topupPayChannelBusiness;
	
	@Autowired
	private Util util;
	
	@Value("${payment.ipAddress}")
	private String ipAddress;
	
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/paymentchannel/topup/tosdjshops")
	public  String toRegistershops(HttpServletRequest request, HttpServletResponse response, Model model)throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String bankName = request.getParameter("bankName");
		String bankNo = request.getParameter("bankNo");
		String amount = request.getParameter("amount");
		String ordercode = request.getParameter("ordercode");
		String provinceOfBank = request.getParameter("provinceOfBank");
		String cityOfBank = request.getParameter("cityOfBank");
		String bankBranchName = request.getParameter("bankBranchName");
		String bankBranchId = request.getParameter("bankBranchId");
		
		model.addAttribute("bankName", bankName);
		model.addAttribute("bankNo",bankNo);
		model.addAttribute("amount", amount);
		model.addAttribute("ordercode", ordercode);
		model.addAttribute("provinceOfBank", provinceOfBank);
		model.addAttribute("cityOfBank", cityOfBank);
		model.addAttribute("bankBranchName", bankBranchName);
		model.addAttribute("bankBranchId", bankBranchId);
		
		return "shops";
	}
	
	
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/paymentchannel/topup/sdjshops")
	public  String returnRegistershops(HttpServletRequest request, HttpServletResponse response, Model model)throws IOException {
		
		Map map = new HashMap();
		// 设置编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String provinceOfShop = request.getParameter("provinceOfShop");
		String cityOfShop = request.getParameter("cityOfShop");
		String companyType = request.getParameter("company_type");
		String shortName = request.getParameter("short_name");
		String registeredCapital = request.getParameter("registered_capital");
		String creationDate = request.getParameter("creation_date");
		String businessLicenseno = request.getParameter("business_licenseno");
		String bankName = request.getParameter("bankName");
		String bankNo = request.getParameter("bankNo");
		String amount = request.getParameter("amount");
		String ordercode = request.getParameter("ordercode");
		String provinceOfBank = request.getParameter("provinceOfBank");
		String cityOfBank = request.getParameter("cityOfBank");
		String bankBranchName = request.getParameter("bankBranchName");
		String bankBranchId = request.getParameter("bankBranchId");
		LOG.info("接口/v1.0/paymentchannel/topup/sdjshops——@companyType="+companyType+"@shortName="+shortName+"@registeredCapital="+registeredCapital+"@creationDate="+creationDate+"@provinceOfBank="+provinceOfBank+"@cityOfBank="+cityOfBank+"@businessLicenseno="+businessLicenseno
				+"@provinceOfShop="+provinceOfShop+"@cityOfShop="+cityOfShop+"@bankName="+bankName+"@bankNo="+bankNo+"@bankBranchName="+bankBranchName+"@amount="+amount+"@ordercode="+ordercode+"@bankBranchId="+bankBranchId);
		if (StringUtils.isEmpty(businessLicenseno)) {
			model.addAttribute("RespCode", "9999");
			model.addAttribute("RespMsg", "没有营业执照编号");
			 return "ailongpay_err";
		} else {
			model.addAttribute("companyType", companyType);
			model.addAttribute("shortName",shortName);
			model.addAttribute("businessLicenseno", businessLicenseno);
			model.addAttribute("registeredCapital", registeredCapital);
			model.addAttribute("provinceOfShop", provinceOfShop);
			model.addAttribute("cityOfShop", cityOfShop);
			model.addAttribute("creationDate", creationDate);
			model.addAttribute("bankName", bankName);
			model.addAttribute("bankNo",bankNo);
			model.addAttribute("amount", amount);
			model.addAttribute("ordercode", ordercode);
			model.addAttribute("provinceOfBank", provinceOfBank);
			model.addAttribute("cityOfBank", cityOfBank);
			model.addAttribute("bankBranchName", bankBranchName);
			model.addAttribute("bankBranchId", bankBranchId);
			return "sdjbankInfo";
		}
		
	}
	
	
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/paymentchannel/topup/sdjshops1")
	public @ResponseBody Object topupRequest(HttpServletRequest request,
			@RequestParam(value = "bankBranchName") String bankBranchName,
			@RequestParam(value = "bankBranchId") String bankBranchId,
			@RequestParam(value = "provinceOfBank") String provinceOfBank,
			@RequestParam(value = "cityOfBank") String cityOfBank,
			@RequestParam(value = "bankName") String bankName,
			@RequestParam(value = "amount") String amount,
			@RequestParam(value = "company_type") String company_type,
			@RequestParam(value = "ordercode") String ordercode,
			@RequestParam(value = "short_name") String short_name,
			@RequestParam(value = "business_licenseno") String business_licenseno,
			@RequestParam(value = "registered_capital") String registered_capital,
			@RequestParam(value = "provinceOfShop") String provinceOfShop,
			@RequestParam(value = "cityOfShop") String cityOfShop,
			@RequestParam(value = "creation_date") String creation_date,
			@RequestParam(value = "bankNo") String bankNo
			) throws Exception {
		
		Map mapp = new HashMap();
		mapp.put("resp_code", "success");	//URLEncoder.encode(,"UTF-8")
		mapp.put("channel_type", "sdj");
		mapp.put("redirect_url", ipAddress+"/v1.0/paymentchannel/topup/sdjshops"+"?company_type="+company_type+"&short_name="+URLEncoder.encode(short_name,"UTF-8")+"&registered_capital="+registered_capital+"&creation_date="+creation_date+"&provinceOfBank="+URLEncoder.encode(provinceOfBank,"UTF-8")+"&cityOfBank="+URLEncoder.encode(cityOfBank,"UTF-8")+"&business_licenseno="+business_licenseno
				+"&provinceOfShop="+URLEncoder.encode(provinceOfShop,"UTF-8")+"&cityOfShop="+URLEncoder.encode(cityOfShop,"UTF-8")+"&bankName="+URLEncoder.encode(bankName,"UTF-8")+"&bankNo="+bankNo+"&bankBranchName="+URLEncoder.encode(bankBranchName,"UTF-8")+"&amount="+amount+"&ordercode="+ordercode+"&bankBranchId="+bankBranchId);
		return mapp;
		
		
	}
	
	
	
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/paymentchannel/topup/sdjbindcard")
	public  String returnpay(HttpServletRequest request, HttpServletResponse response, Model model)throws IOException {
		// 设置编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		
		model.addAttribute("ordercode", request.getParameter("ordercode"));
		
		RestTemplate restTemplate=new RestTemplate();
		URI uri = util.getServiceUrl("transactionclear", "error url request!");
		String url = uri.toString() + "/v1.0/transactionclear/payment/query/ordercode";
		MultiValueMap<String, String> requestEntity  = new LinkedMultiValueMap<String, String>();
		requestEntity.add("order_code", request.getParameter("ordercode"));
		String result = restTemplate.postForObject(url, requestEntity, String.class);
		LOG.info("RESULT================"+result);
		JSONObject jsonObject =  JSONObject.fromObject(result);
		JSONObject resultObj  =  jsonObject.getJSONObject("result");
		String userid = resultObj.getString("userid");
		//银行卡
		String bankCard = resultObj.getString("bankcard");
		
		restTemplate=new RestTemplate();
		uri = util.getServiceUrl("user", "error url request!");
		url = uri.toString() + "/v1.0/user/bank/default/cardno";
		requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("cardno", bankCard);
		requestEntity.add("type", "0");
		result = restTemplate.postForObject(url, requestEntity, String.class);
		LOG.info("接口/v1.0/user/bank/default/cardno--RESULT================"+result);
		jsonObject =  JSONObject.fromObject(result);
		resultObj  =  jsonObject.getJSONObject("result");
		
		String bankBranchName=null;
		String bankName=null;
		String bankBranchId=null;
		String province=null;
		String city=null;
		
		
		try {
			bankName = resultObj.getString("bankName");
			bankBranchName = resultObj.getString("bankBranchName");
			bankBranchId = resultObj.getString("lineNo");
			province = resultObj.getString("province");
			city = resultObj.getString("city");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//将银行名称进行更改
		String bankname = Util.queryBankNameByBranchName(bankName);
				
		if(bankBranchName==null||"".equals(bankBranchName)||"null".equals(bankBranchName)){
				bankBranchName = "";
		}
	
		
		model.addAttribute("bankName", bankname);
		model.addAttribute("bankNo",bankCard);
		model.addAttribute("province", province);
		model.addAttribute("city", city);
		model.addAttribute("bankBranchName", bankBranchName);
		model.addAttribute("bankBranchId", bankBranchId);
		

		SDJBindCard sdjBindCrad = topupPayChannelBusiness.getBankCard(userid,bankCard);
		
		if(sdjBindCrad==null){
			return "sdjbindcard";
		}else{
			return "paymessage";
		}
		
		
	}
	
	
	
	
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/paymentchannel/topup/sdjbindcardsuccess")
	public  String sdjbindcardsuccess(HttpServletRequest request, HttpServletResponse response, Model model)throws IOException {
		// 设置编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		
		model.addAttribute("ordercode", request.getParameter("ordercode"));
		
		
		return "paymessage";
		
	}
	
	
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/paymentchannel/topup/tobindcardmessage")
	public  String tobindcardmessage(HttpServletRequest request, HttpServletResponse response, Model model)throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String ordercode = request.getParameter("ordercode");
		
		model.addAttribute("ordercode", ordercode);
		
		return "bindcardmessage";
	}
	
	
	
	
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/paymentchannel/topup/sdjpaysuccess")
	public  String returnpaysuccess(HttpServletRequest request, HttpServletResponse response, Model model)throws IOException {
		// 设置编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
	
		return "sdjsuccess";
	}
	
	
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/paymentchannel/topup/sdjpayerror")
	public  String returnpayerror(HttpServletRequest request, HttpServletResponse response, Model model)throws IOException {
		// 设置编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
	
		return "sdjerror";
	}
	
}
	
